package com.laikan.library.httpvolley.engine;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.request.HttpGetRequest;
import com.laikan.library.httpvolley.request.HttpPostRequest;
import com.laikan.library.httpvolley.request.HttpRequest;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.utils.AndroidUtils;
import com.socks.library.KLog;

import java.util.Map;

/**
 * 网络请求发起者 的基类
 *
 * @author anrun
 */
class BaseHttpRequester implements Listener<String>, ErrorListener {

    protected RequestListener requestListener;// 监听
    private RequestQueue requestQueue;// velloy 请求队列
    private HttpRequest httpRequest;

    static BaseHttpRequester newInstance(RequestQueue requestQueue, RequestListener requestListener) {

        return new BaseHttpRequester(requestQueue, requestListener);
    }

    private BaseHttpRequester(RequestQueue requestQueue, RequestListener requestListener) {

        this.requestQueue = requestQueue;
        this.requestListener = requestListener;
    }

    private HttpGetRequest createCommonGetRequest(Requester requester) {

        Object requestTag = requester.getRequestTag();
        HttpGetRequest getRequest = new HttpGetRequest(requester, this, this);
        getRequest.setHeaders(requester.getRequesterHeaders());
        // 设置请求 tag
        if (requestTag != null) {
            getRequest.setTag(requestTag);
        }
        this.httpRequest = getRequest;
        return getRequest;
    }

    private HttpPostRequest createCommonPostRequest(Requester requester) {

        Object requestTag = requester.getRequestTag();
        HttpPostRequest postRequest = new HttpPostRequest(requester, this, this);
        postRequest.setHeaders(requester.getRequesterHeaders());
        // 设置请求 tag
        if (requestTag != null) {
            postRequest.setTag(requestTag);
        }
        this.httpRequest = postRequest;
        return postRequest;
    }

    void doCommonGetRequest(Requester requester) {
        requestListener.onPreExecute();
        HttpRequest request = createCommonGetRequest(requester);
        doStringRequest(request);
    }

    void doCommonPostRequest(Requester requester) {
        requestListener.onPreExecute();
        HttpRequest request = createCommonPostRequest(requester);
        doStringRequest(request);
    }

    private void doStringRequest(HttpRequest request) {

        if (request.getMethod() == Request.Method.POST) {
            KLog.i("post->Url： " + request.getUrl());
            Map<String, String> params = ((HttpPostRequest)request).getRequestParams();
            KLog.i("map->Paramters： " + AndroidUtils.encodeParameters(params, "UTF-8"));
            KLog.i("json->Paramters： " + ((HttpPostRequest)request).getmJsonBody());
        }else if (request.getMethod() == Request.Method.GET) {
            KLog.i("get->Url： " + request.getUrl());
        }
        httpRequest.setStartRequestTime(System.currentTimeMillis());
        requestQueue.add(request);
    }

    // 这是一层监听过滤
    @Override
    public void onResponse(String response) {

        httpRequest.setEndRequestTime(System.currentTimeMillis());
        long takeTime = httpRequest.getEndRequestTime() - httpRequest.getStartRequestTime();
        KLog.d("耗时:" + takeTime + " ms");
        KLog.json(response);
        if (requestListener != null) {
            HttpResponse httpResponse = new HttpResponse();
            httpResponse.responseHeader = httpRequest.getResponseHeaders();
            httpResponse.responseContent = response;
            requestListener.onSuccess(httpResponse);
            requestListener.onFinish();
        }
    }

    // 这是一层监听过滤
    @Override
    public void onErrorResponse(VolleyError volleyError) {

        // 可以在此处理一些公共的错误
        if (requestListener != null) {
            VolleyErrorHandler errorHandler = new VolleyErrorHandler(volleyError);
            // 通用的错误提示
            requestListener.onError(errorHandler);
            requestListener.onFinish();
        }
    }
}
